/*
 * COPYRIGHT. ShenZhen iByte Technology Co., Ltd. 2018.
 * ALL RIGHTS RESERVED.
 *
 * No part of this publication may be reproduced, stored in a retrieval system, or transmitted,
 * on any form or by any means, electronic, mechanical, photocopying, recording, 
 * or otherwise, without the prior written permission of ShenZhen iByte Network Technology Co., Ltd.
 *
 * Amendment History:
 * 
 * Date                   By              Description
 * -------------------    -----------     -------------------------------------------
 * Dec 3, 2018    Li.shangzhi         Create the class
 */
package com.github.icloud.elasticsearch.api.aggregations;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.filters.Filters;
import org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator;
import org.elasticsearch.search.aggregations.bucket.global.Global;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.junit.Test;
import com.github.icloud.elasticsearch.ElasticsearchClientBase;
/**
 * @FileName BucketAggregations.java
 * @Description: 桶分聚合
 *
 *	官方文档 @see <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/_bucket_aggregations.html'></a>	
 *
 * @Date Dec 3, 2018 4:13:35 PM
 * @author Li.shangzhi
 * @version 1.0
 */
public class BucketAggregations extends ElasticsearchClientBase{

	@Test
	public void  iEsBucketAggregations() throws Exception{

		SearchResponse sr = client.prepareSearch("icloud-iot-log")
				.setQuery(QueryBuilders.matchAllQuery())
				.addAggregation(
						AggregationBuilders
						.global("agg")
						.subAggregation(
								AggregationBuilders.terms("ages").field("age")
								)
						)
						.get();
		Global agg = sr.getAggregations().get("agg");
		// 总数统计
		long count = agg.getDocCount(); 
		System.out.println("global count:" + count);
		// 分组统计
		Terms terms = sr.getAggregations().get("agg");
		for (Terms.Bucket bucket:terms.getBuckets()) {
			System.out.println(bucket.getKey()+">>"+bucket.getDocCount());
		}
	}
	
	@Test
	public void iEsFilterAggregation() throws Exception{
		AggregationBuilder aggregation =
                AggregationBuilders
                        .filters("agg",
                                new FiltersAggregator.KeyedFilter("ibyte count", QueryBuilders.termQuery("user", "ibyte")),
                                new FiltersAggregator.KeyedFilter("lishangzhi count", QueryBuilders.termQuery("user", "lishangzhi")));

        SearchResponse sr = client.prepareSearch("icloud-iot-log")
                .addAggregation(aggregation)
                .get();

        Filters agg = sr.getAggregations().get("agg");

        for (Filters.Bucket entry : agg.getBuckets()) {
            String key = entry.getKeyAsString();            // bucket key
            long docCount = entry.getDocCount();            // Doc count

            System.out.println("global " + key + " count:" + docCount);
        }
	}
}
